package com.chhuang.ewcrm.controller;

import com.chhuang.ewcrm.bean.QueryBean;
import com.chhuang.ewcrm.entity.ESellGoods;
import com.chhuang.ewcrm.entity.ESell;
import com.chhuang.ewcrm.service.ESellGoodsService;
import com.chhuang.ewcrm.service.ESellService;
import com.chhuang.ewcrm.string.MoneyUtil;
import com.chhuang.ewcrm.vo.ResultBean;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * (ESell)表控制层
 *
 * @author chhuang
 * @since 2020-11-15 16:21:24
 */
@RestController
@RequestMapping("/service/eSell")
public class ESellController {
    Logger logger = LoggerFactory.getLogger(ESellController.class);
    /**
     * 服务对象
     */
    @Resource
    private ESellService eSellService;
    @Resource
    private ESellGoodsService eSellGoodsService;

    /**
     * 确认记录完成
     * @param sellId
     * @param customerNetWeight
     * @param sellPrice
     * @param freightCosts
     * @param deductionMoney
     * @param remark2
     * @return
     */
    @PostMapping("/confirmCompletion")
    public ResultBean<?> confirmCompletion(String sellId, Double customerNetWeight, Double sellPrice,
                                           Double freightCosts, Double deductionMoney, String remark2){
        ResultBean<?> resultBean = new ResultBean<>();
        resultBean.setSuccess(false);

        if(StringUtils.isNotEmpty(sellId)) {
            ESell eSell = this.eSellService.queryById(sellId);
            if (eSell != null) {
                if (eSell.getIsDelivered()) {//是送到
                    eSell.setCustomerNetWeight(customerNetWeight);
                    eSell.setSellPrice(sellPrice);
                    eSell.setFreightCosts(freightCosts);
                    eSell.setDeductionMoney(deductionMoney);

                    eSell.setPaymentAmount(eSell.getCustomerNetWeight()*eSell.getSellPrice()-(eSell.getDeductionMoney()==null?0:eSell.getDeductionMoney()));
                    eSell.setPaymentAmountCh(MoneyUtil.moneyToChinese(BigDecimal.valueOf(eSell.getPaymentAmount())));
                    eSell.setTotalAmount(eSell.getPaymentAmount()-eSell.getFreightCosts());
                    eSell.setSellAveragePrice(eSell.getTotalAmount()/eSell.getNetWeight());
                }
                eSell.setRemark2(remark2);
                eSell.setStatus(2);
                Date now = new Date();
                eSell.setModifyTime(now);
                this.eSellService.update(eSell);
                resultBean.setSuccess(true);

                ESellGoods goods = eSellGoodsService.queryById(eSell.getSellGoodsId());
                if(goods!=null){
                    goods.setSellTotalWeight(goods.getSellTotalWeight()+eSell.getNetWeight());
                    goods.setSellTotalAmount(goods.getSellTotalAmount()+eSell.getTotalAmount());
                    goods.setSellAveragePrice(goods.getSellTotalAmount()/goods.getSellTotalWeight());
                    goods.setModifyTime(now);
                    eSellGoodsService.update(goods);
                }else {
                    logger.error("添加的新销售记录，对应的物品已经不存在");
                }
                
            }else {
                resultBean.setMessage("称重记录不存在");
            }
        }else {
            resultBean.setMessage("参数不为空");
        }

        return resultBean;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param sellId 主键
     * @return 单条数据
     */
    @GetMapping("/getOne")
    public ResultBean<ESell> getOne(String sellId) {
        ResultBean<ESell> resultBean = new ResultBean<>();
        resultBean.setSuccess(false);
        if(StringUtils.isNotEmpty(sellId)){
            resultBean.setSuccess(true);
            resultBean.setData(this.eSellService.queryById(sellId));
        }else{
            resultBean.setMessage("参数不为空");
        }
        logger.debug(resultBean.toString());
        return resultBean;
    }

    /**
     * 表格查询
     * @param queryBean
     * @return
     */
    @PostMapping("/queryPage")
    public ResultBean<PageInfo<ESell>> query(@RequestBody QueryBean<ESell> queryBean){
        logger.debug(queryBean.toString());
        ResultBean<PageInfo<ESell>> resultBean = new ResultBean<>();

        PageInfo<ESell> pageInfo = null;
        List<ESell> sells = null;
        if(queryBean!=null) {
            if (StringUtils.isNotEmpty(queryBean.getOrderBy()))
                pageInfo = eSellService.search(queryBean.getSearch(), queryBean.getPageNum(), queryBean.getPageSize(), queryBean.getOrderBy());
            else
                pageInfo = eSellService.search(queryBean.getSearch(), queryBean.getPageNum(), queryBean.getPageSize());
            resultBean.setData(pageInfo);
            resultBean.setSuccess(true);
            resultBean.setCode(queryBean.getDraw());
        }else {
            resultBean.setSuccess(false);
            resultBean.setMessage("参数不为空");
        }

        logger.debug(resultBean.toString());
        return resultBean;
    }

}